package com.service.admin.base.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.service.admin.base.controller.req.BannerSaveReq;
import com.service.admin.base.service.BannerService;
import com.service.core.exception.CommonException;
import com.service.dao.enums.CommonStatusEnums;
import com.service.dao.mapper.BannerMapper;
import com.service.dao.model.Banner;
import com.service.utility.modelmapper.ModelMapperUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Date;

/**
 * <dl>
 * <dt>platform</dt>
 * <dd>Description:</dd>
 * <dd>Copyright: Copyright (C) 2015</dd>
 * <dd>Company: </dd>
 * <dd>CreateDate: 2019年05月13日</dd>
 * </dl>
 *
 * @author
 */
@Slf4j
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements BannerService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(BannerSaveReq bannerSaveReq) {
        Banner banner = ModelMapperUtil.map(bannerSaveReq, Banner.class);
        banner.setId(null);
        banner.setBannerStatus(CommonStatusEnums.ACTIVE.getCode());
        banner.setCreateTime(new Date());
        save(banner);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(BannerSaveReq bannerSaveReq) {
        if (ObjectUtils.isEmpty(getById(bannerSaveReq.getId()))) {
            throw new CommonException("无此记录");
        }
        updateById(ModelMapperUtil.map(bannerSaveReq, Banner.class));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBanner(Integer id) {
        if (ObjectUtils.isEmpty(getById(id))) {
            throw new CommonException("无此记录");
        }
        updateById(Banner.builder().id(id).bannerStatus(CommonStatusEnums.LOCK.getCode()).build());
    }
}
